package com.codeZ.CompositePattern.actualCombat;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件夹节点
 *
 * @author codeZhang
 * @Date 2024/8/15 11:10
 */
public class Composite extends Component {

    public Composite(String name) {
        super.name = name;
    }

    private final List<Component> componentList = new ArrayList<>();

    @Override
    public void add(Component component) {
        componentList.add(component);
    }

    @Override
    public void remove(Component component) {
        componentList.remove(component);
    }

    @Override
    public void display(int depth) {
        for (int i = 0; i < depth; i++){
            System.out.print("-");
        }
        System.out.println(getName());
        for (Component component : componentList) {
            component.display(depth + 2);
        }
    }

    @Override
    public String getName() {
        return super.name;
    }

    @Override
    public long getSize() {
        return componentList.stream().mapToLong(Component::getSize).sum();
    }
}
